package com.example.delicioushunter.view.activity;

import com.example.delicioushunter.R;
import com.example.delicioushunter.view.adapter.FoodPagerAdapter;
import com.example.delicioushunter.view.customview.MyViewPager;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class EditFoodActivity extends FragmentActivity {

	private MyViewPager pager;
	private FoodPagerAdapter adapter;
	private ActionBar actionBar;
	private String[] tabs = { "Materials", "Methods" };
	private int _id;

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_food);

		Intent intent = getIntent();
		_id = Integer.parseInt(intent.getExtras().getString("ID"));

		pager = (MyViewPager) findViewById(R.id.pager);
		actionBar = this.getActionBar();
		adapter = new FoodPagerAdapter(getSupportFragmentManager());

		pager.setAdapter(adapter);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.addTab(
				actionBar.newTab().setText(tabs[0])
						.setTabListener(mMaterialTabListener), false);
		actionBar.addTab(
				actionBar.newTab().setText(tabs[1])
						.setTabListener(mInfoTabListener), true);

		pager.setCurrentItem(1);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private TabListener mMaterialTabListener = new TabListener() {

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			pager.setCurrentItem(0);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}
	};

	private TabListener mInfoTabListener = new TabListener() {

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			pager.setPagingEnabled(true);
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			pager.setCurrentItem(1);
			pager.setPagingEnabled(false);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub
		}
	};

	@Override
	public void onBackPressed() {
		Intent data = new Intent();
		data.putExtra("FOOD_ID", String.valueOf(_id));
		setResult(RESULT_OK, data);
		finish();
	}
}
